package com.guigu.www.tangyingmeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.mapper.BespeakResultMapper;
import com.guigu.www.mapper.HealthManagerMapper;
import com.guigu.www.mapper.ReturnDiaryMapper;
import com.guigu.www.pojo.*;
import com.guigu.www.tangyingmeng.service.TymReturnDraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class TymReturnDraryServiceImpl implements TymReturnDraryService {

    @Autowired
    private ReturnDiaryMapper diaryMapper;
    @Autowired
    private BespeakResultMapper resultMapper;
    @Autowired
    private HealthManagerMapper managerMapper;

    @Override
    public PageVo<ReturnDiary> seldiaryall(Map<String, String> map) {
        QueryWrapper<ReturnDiary> wrapper = new QueryWrapper<>();
        wrapper.like("re_diary_id",map.get("findid")).like("bes_result_id", map.get("findreid"));
        IPage<ReturnDiary> page = new Page<>(Integer.valueOf(map.get("index")), Integer.valueOf(map.get("pageSize")));
        page = diaryMapper.selectPage(page, wrapper);
        PageVo<ReturnDiary> pageVo = new PageVo<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(page.getRecords());
        for (ReturnDiary diary : pageVo.getRows()) {
            diary.setResult(resultMapper.selectById(diary.getBesResultId()));
            diary.setManager(managerMapper.selectById(diary.getHmId()));
        }
        return pageVo;
    }

    @Override
    public ReturnDiary seldiaryone(Integer id) {
        ReturnDiary diary = diaryMapper.selectById(id);
        return diary;
    }

    @Override
    public List<BespeakResult> selresultall() {
        QueryWrapper<BespeakResult> wrapper = new QueryWrapper<>();
        wrapper.eq("bes_result_health",1).eq("bes_result_confirm",1);
        List<BespeakResult> list = resultMapper.selectList(wrapper);
        return list;
    }

    @Override
    public Boolean adddiary(ReturnDiary diary) {
        /*String date = diary.getReDiaryTime().substring(0,diary.getReDiaryTime().indexOf("T"));
        String time = diary.getReDiaryTime().substring(diary.getReDiaryTime().indexOf("T")+1,diary.getReDiaryTime().indexOf("."));
        diary.setReDiaryTime(date+" "+time);*/
        int insert = diaryMapper.insert(diary);
        if(insert>0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean updiary(ReturnDiary diary) {
        /*if(diary.getReDiaryTime().indexOf("T")>0){
            String date = diary.getReDiaryTime().substring(0,diary.getReDiaryTime().indexOf("T"));
            String time = diary.getReDiaryTime().substring(diary.getReDiaryTime().indexOf("T")+1,diary.getReDiaryTime().indexOf("."));
            diary.setReDiaryTime(date+" "+time);
        }else {
           diary.setReDiaryTime(diary.getReDiaryTime().substring(0,diary.getReDiaryTime().indexOf(".")));
        }*/
        int update = diaryMapper.updateById(diary);
        if(update>0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean deldiary(Integer id) {
        int delete = diaryMapper.deleteById(id);
        if(delete>0) {
            return true;
        }else {
            return false;
        }
    }
}
